/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import entities.Acteur;
import entities.Film;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import session.GestionnaireDeFilms;
import session.GestionnaireActeurs;

/**
 *
 * @author asus
 */
@WebServlet(name = "ServletFilm", urlPatterns = {"/ServletFilm"})
public class ServletFilm extends HttpServlet {

    @EJB
    private GestionnaireDeFilms gestionnaireDeFilms;
    @EJB
    private GestionnaireActeurs gestionnaireActeurs;
    private int firstResult = 0;

    /*
     * public void init() { // appelé lors de la première invocation de la
     * Servlet gestionnaireDeFilms.creerFilmsDeTest(); }
     */
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter("action");
        String categorie = request.getParameter("categorie");
        String titre = request.getParameter("titre");
        String id = request.getParameter("id");
        String orderBy = request.getParameter("orderBy");
        String nomActeur = request.getParameter("nomActeur");
        String affiche = request.getParameter("affiche");
        String prixdvd = request.getParameter("prixdvd");
        String prixblueray = request.getParameter("prixblueray");
         HttpSession session = request.getSession(true);

        if ((action != null) && (!action.equals(""))) {
            if (action.equals("listerFilms")) {
               
                if (categorie != null) {
                    // la valeur pour tout lister sera "all"
                    if (categorie.equals("all")) {
                          if (request.getParameter("firstResult") != null) {
                        firstResult = Integer.parseInt(request.getParameter("firstResult"));
                 }
                        Collection<Film> liste = gestionnaireDeFilms.getAllFilmsPagination(firstResult);
                        request.setAttribute("listeDesFilms", liste);
                        // Ici on va utiliser browse.jsp pour afficher les résultats
                        RequestDispatcher dp = request.getRequestDispatcher("all.jsp?action=listerFilms&firstResult=" + firstResult);
                        dp.forward(request, response);
                        // test si parmis les categories de film celle contenue dans 'categorie' existe
                    } else if (categorie.equals("null")) {
                        RequestDispatcher dp = request.getRequestDispatcher("browse.jsp");
                        dp.forward(request, response);
                    } else if (gestionnaireDeFilms.getAllCategorie().contains(categorie)) {
                        Collection<Film> liste = gestionnaireDeFilms.getAllFilmsByCategorie(categorie);
                        request.setAttribute("listeDesFilms", liste);
                        RequestDispatcher dp = request.getRequestDispatcher("browse.jsp?action=listerFilms");
                        dp.forward(request, response);
                    }
                } else if (titre != null) {
                    Collection<Film> liste = gestionnaireDeFilms.findFilmsByTitle(titre);
                    request.setAttribute("listeDesFilms", liste);
                    RequestDispatcher dp = request.getRequestDispatcher("browse.jsp?action=listerFilms");
                    dp.forward(request, response);

                } else if (orderBy.equals("titre")) {
                    Collection<Film> liste = gestionnaireDeFilms.getAllFilmsOrderByTitle();
                    request.setAttribute("listeDesFilms", liste);
                    RequestDispatcher dp = request.getRequestDispatcher("all.jsp?action=listerFilms");
                    dp.forward(request, response);
                } else if (orderBy.equals("note")) {
                    Collection<Film> liste = gestionnaireDeFilms.getAllFilmsOrderByNote();
                    request.setAttribute("listeDesFilms", liste);
                    RequestDispatcher dp = request.getRequestDispatcher("all.jsp?action=listerFilms");
                    dp.forward(request, response);
                }

            } else if ((action.equals("listerFilmsDetails")) && (id != null)) {
                Collection<Film> liste = new ArrayList<Film>();
                liste.add(gestionnaireDeFilms.findFilmById(id));
                request.setAttribute("listeDesFilms", liste);
                RequestDispatcher dp = request.getRequestDispatcher("all.jsp?action=listerFilmsDetails");
                dp.forward(request, response);
            } else if ((action.equals("acteur")) && (nomActeur != null)) {
                Collection<Acteur> liste = gestionnaireActeurs.findActeurByName(nomActeur);
                Collection<Film> listeFilm = gestionnaireDeFilms.findFilmsByActor(nomActeur);
                request.setAttribute("acteur", liste);
                request.setAttribute("acteurFilm", listeFilm);
                RequestDispatcher dp = request.getRequestDispatcher("all.jsp?action=acteur");
                dp.forward(request, response);

            }
            else if (action.equals("popup")){
                session.setAttribute("affiche",affiche);
                session.setAttribute("dvd",prixdvd);
                session.setAttribute("blueray",prixblueray);
                session.setAttribute("titre",titre);
                System.out.println("je charge la popup "+titre+affiche);
                request.getRequestDispatcher("popup.jsp").forward(request, response);
                       
            } 
        } else {
            RequestDispatcher dp = request.getRequestDispatcher("index.jsp");
            dp.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}